<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('financeModule.repaymentNotice.submitTime')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('financeModule.repaymentNotice.state')">
          <el-select v-model="searchForm.status" clearable :placeholder="$t('common.select')" style="width: 100%">
            <el-option
              v-for="item in $t('financeModule.repaymentNotice.solve')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="inputData" :placeholder="$t('common.input')" class="input-with-select">
            <el-select slot="prepend" v-model="selectData">
              <el-option
                v-for="(item, index) in $t('financeModule.repaymentNotice.search')"
                :key="index"
                :label="item.desc"
                :value="item.value"
              />
            </el-select>
          </el-input>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;"
      >
        <el-table-column
          prop="createTime"
          :label="tableText['submitTime']"
          min-width="170"
        />
        <el-table-column
          prop="userAccount"
          :label="tableText['userAccount']"
          min-width="150"
        />
        <el-table-column
          prop="username"
          :label="tableText['userName']"
          min-width="150"
        />
        <el-table-column
          prop="orderNo"
          :label="tableText['loanOrderNo']"
          min-width="150"
        />
        <el-table-column
          prop="status"
          :label="tableText['state']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['status']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          :label="tableText['remark']"
          min-width="100"
        />
        <el-table-column
          prop="modifier"
          :label="tableText['operateName']"
          min-width="100"
        />
        <el-table-column
          prop="modifyTime"
          :label="tableText['operateTime']"
          min-width="170"
        />
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" :loading="editLoading" @click="editItem(scope.row.id)">{{ tableText['show'] }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <repayment-notice-dialog
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
  </el-row>
</template>

<script>
import { getRepayProofs, getThisRepaymentNotice, simpleInfo } from '@/api/finance'
import RepaymentNoticeDialog from '@/components/finance/repayment-notice-dialog'
import { formatDate } from '@/utils/method'
export default {
  name: 'RepaymentNotice',
  components: {
    RepaymentNoticeDialog
  },
  data() {
    return {
      editDialog: false,
      editLoading: false,
      loading: false,
      tableData: [],
      tableText: {},
      time: null,
      totalNumber: 0,
      searchForm: this.params(),
      inputData: null,
      selectData: 'orderNo',
      chooseItem: {
        id: null,
        userId: null,
        userImg: null,
        userAccount: null,
        orderNo: null,
        repayType: null,
        repayMoney: null,
        repayProof: null,
        status: null,
        remark: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('financeModule.repaymentNotice.table')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        startDate: null,
        endDate: null,
        status: '',
        orderNo: null,
        tel: null,
        username: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.selectData = 'orderNo'
      this.inputData = null
      this.time = null
      this.getData()
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    searchData() {
      const data = {
        currentPage: this.searchForm.currentPage,
        pageSize: this.searchForm.pageSize,
        status: this.searchForm.status
      }
      if (this.time && this.time.length > 1) {
        data['startDate'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['endDate'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      }
      if (this.selectData && this.inputData) {
        data[this.selectData] = this.inputData
      }
      return data
    },
    getData() {
      this.loading = true
      getRepayProofs(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    add() {
      this.choose({})
      this.editDialog = true
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.userId = param.userId
      this.chooseItem.userImg = param.userImg
      this.chooseItem.userAccount = param.userAccount
      this.chooseItem.orderNo = param.orderNo
      this.chooseItem.repayType = param.repayType
      this.chooseItem.repayMoney = param.repayMoney
      this.chooseItem.repayProof = param.repayProof
      this.chooseItem.status = param.status
      this.chooseItem.remark = param.remark
    },
    headerImg(id) {
      simpleInfo(id)
        .then(res => {
          return res.success && res.data && res.data['headPortrait'] ? res.data['headPortrait'] : null
        })
        .catch(err => {
          this.editLoading = false
          this.$message.error(err.message)
        })
    },
    editItem(id) {
      this.editLoading = true
      getThisRepaymentNotice(id)
        .then(res => {
          if (res.success) {
            const image = this.headerImg(id)
            const data = res.data
            data.userImg = image
            this.choose(data)
            this.editDialog = true
            this.editLoading = false
          } else {
            this.editLoading = false
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    changeDialog(param) {
      this.editDialog = param
      setTimeout(() => {
        this.choose({})
      }, 10)
    },
    editSuccess() {
      this.getData()
    },
    setValue(data) {
      const map = this.tableText['solve']
      const name = map.filter(item => {
        return item.value.toString() === data.toString()
      })
      return (name.length > 0 && name[0].desc) ? name[0].desc : ''
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
